package com.uyayo.notepad.service.impl;

import com.uyayo.framework.common.exception.ParentNotFoundException;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.notepad.entity.FavoriteCategory;
import com.uyayo.notepad.mapper.FavoriteCategoryMapper;
import com.uyayo.notepad.model.FavoriteCategoryCondition;
import com.uyayo.notepad.service.FavoriteCategoryService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;


@Service("favoriteCategoryService")
public class FavoriteCategoryServiceImpl extends BaseServiceImpl<FavoriteCategory> implements FavoriteCategoryService {

	private final FavoriteCategoryMapper favoriteCategoryMapper;

	public FavoriteCategoryServiceImpl(FavoriteCategoryMapper favoriteCategoryMapper) {
		this.favoriteCategoryMapper = favoriteCategoryMapper;
	}

	@Override
	public List<FavoriteCategory> selectFavoriteCategory(FavoriteCategoryCondition favoriteCategoryCondition) {
		Example example = new Example(FavoriteCategory.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(favoriteCategoryCondition);
		example.createCriteria();
		return favoriteCategoryMapper.selectByExample(example);
	}

	@Override
	public int selectCountByParentId(Long parentId) {
		FavoriteCategory favoriteCategory = new FavoriteCategory();
		favoriteCategory.setParentId(parentId);
		return favoriteCategoryMapper.selectCount(favoriteCategory);
	}

	@Override
	public int saveFavoriteCategory(FavoriteCategory favoriteCategory) throws ParentNotFoundException {
		Long parentId = favoriteCategory.getParentId();
		if(!Objects.isNull(parentId)){
			FavoriteCategory favoriteCategoryParent = selectByKey(parentId);
			if(favoriteCategoryParent==null){
				throw new ParentNotFoundException("Parent not found!");
			}
		}
		int count = favoriteCategoryMapper.insertSelective(favoriteCategory);
		if(count >0){
			updateHasChildrennToParent(parentId);
		}
		return count;
	}

	/**
	 * 迭代检查选择的上级id是否是自己的子级
	 * @param parentIdOld 原上级id
	 * @param parentIdNew	新上级id
	 * @param favoriteCategoryChild 子资源
	 * @param favoriteCategoryParent 父资源
	 */
	private void recursiveCheckParentIsChildren(Long parentIdOld, Long parentIdNew, FavoriteCategory favoriteCategoryChild, FavoriteCategory favoriteCategoryParent){
		if (selectCountByParentId(favoriteCategoryChild.getId()) > 0) {
			Example example = new Example(FavoriteCategory.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("parentId",favoriteCategoryChild.getId());
			List<FavoriteCategory> favoriteCategorys = favoriteCategoryMapper.selectByExample(example);
			if (!favoriteCategorys.isEmpty()) {
				for(FavoriteCategory favoriteCategory:favoriteCategorys){
					if(Objects.equals(favoriteCategory.getId(),parentIdNew)){
						favoriteCategoryParent.setParentId(parentIdOld);
						favoriteCategoryMapper.updateByPrimaryKeySelective(favoriteCategoryParent);
					}else {
						recursiveCheckParentIsChildren(parentIdOld, parentIdNew, favoriteCategory, favoriteCategoryParent);
					}
				}
			}
		}
	}
	@Override
	public int updateFavoriteCategory(FavoriteCategory favoriteCategory) throws ParentNotFoundException {

		FavoriteCategory favoriteCategoryOld = selectByKey(favoriteCategory.getId());

		if(favoriteCategory.getParentId()==null){
			favoriteCategory.setParentId(0L);
		}

		Long parentId = favoriteCategory.getParentId();
		if(parentId!=0){

			//获取修改的父级资源
			FavoriteCategory favoriteCategoryParent = selectByKey(parentId);
			if(favoriteCategoryParent==null){
				throw new ParentNotFoundException("Parent not found!");
			}
			if (selectCountByParentId(favoriteCategoryOld.getId()) > 0) {
				Example example = new Example(FavoriteCategory.class);
				Example.Criteria criteria = example.createCriteria();
				criteria.andEqualTo("parentId",favoriteCategoryOld.getId());
				List<FavoriteCategory> favoriteCategorys = favoriteCategoryMapper.selectByExample(example);
				if (!favoriteCategorys.isEmpty()) {
					for(FavoriteCategory favoriteCategory_:favoriteCategorys){
						if(Objects.equals(favoriteCategory_.getId(),parentId)){
							favoriteCategoryParent.setParentId(favoriteCategoryOld.getParentId());
							favoriteCategoryMapper.updateByPrimaryKeySelective(favoriteCategoryParent);
						}else{
							recursiveCheckParentIsChildren(favoriteCategoryOld.getParentId(),favoriteCategory.getParentId(),favoriteCategory,favoriteCategoryParent);
						}
					}
				}
			}
		}
		if(selectCountByParentId(favoriteCategory.getId())>0) {
			favoriteCategory.setHasChildren(true);
		}else{
			favoriteCategory.setHasChildren(false);
		}
		int count = favoriteCategoryMapper.updateByPrimaryKeySelective(favoriteCategory);
		if(count<=0){
			return count;
		}
		updateHasChildrennToParent(parentId);
		if(favoriteCategoryOld!=null){
			updateHasChildrennToParent(favoriteCategoryOld.getParentId());
		}
		return count;
	}

	/**
	 * 修改父资源是否含子资源
	 * @param parentId 父资源id
	 */
	private void  updateHasChildrennToParent(Long parentId){
		if(!Objects.isNull(parentId)){
			FavoriteCategory favoriteCategory = new FavoriteCategory();
			favoriteCategory.setId(parentId);
			if(selectCountByParentId(parentId)>0) {
				favoriteCategory.setHasChildren(true);
			}else{
				favoriteCategory.setHasChildren(false);
			}
			favoriteCategoryMapper.updateByPrimaryKeySelective(favoriteCategory);
		}
	}

	@Override
	public int deleteFavoriteCategory(Long id) {

		if (selectCountByParentId(id) > 0) {
			Example example = new Example(FavoriteCategory.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("parentId",id);
			List<FavoriteCategory> favoriteCategorys = favoriteCategoryMapper.selectByExample(example);
			if (!favoriteCategorys.isEmpty()) {
				for(FavoriteCategory favoriteCategory:favoriteCategorys){
					recursiveDelete( favoriteCategory);
				}
			}
		}
		return delete(id);
	}

	/**
	 * 递归删除资源
	 * @param favoriteCategory 资源
	 */
	private void recursiveDelete(FavoriteCategory favoriteCategory){

		if (selectCountByParentId(favoriteCategory.getId()) > 0) {
			Example example = new Example(FavoriteCategory.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("parentId",favoriteCategory.getId());
			List<FavoriteCategory> favoriteCategorys = favoriteCategoryMapper.selectByExample(example);
			if (!favoriteCategorys.isEmpty()) {
				for(FavoriteCategory favoriteCategoryChild:favoriteCategorys){
					recursiveDelete(favoriteCategoryChild);
				}
			}
		}
		delete(favoriteCategory.getId());
	}

	public int delete(Long id) {
		FavoriteCategory favoriteCategory = favoriteCategoryMapper.selectByPrimaryKey(id);
		int count = favoriteCategoryMapper.deleteByPrimaryKey(id);
		if(count<=0){
			return count;
		}
		updateHasChildrennToParent(favoriteCategory!=null?favoriteCategory.getParentId():null);
		return count;
	}
}
